

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Xml;
using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Localization;
using JTC.MiniCommerce.Store;

namespace JTC.MiniCommerce.Web.admin {
  public partial class _default : System.Web.UI.Page {

    #region Page Events

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e) {
      try {
        SetDefaulProperties();
        SiteSettings siteSettings = SiteSettings.Load();
        if (siteSettings.CollectSearchTerms) {
          LoadSearchTerms();
        }
        if (!string.IsNullOrEmpty(siteSettings.NewsFeedUrl)) {
          news.NewsFeedUrl = siteSettings.NewsFeedUrl;
        }
        else {
          string newsFeedUrl = ConfigurationManager.AppSettings["defaultNewsFeedUrl"];
          if(!string.IsNullOrEmpty(newsFeedUrl)){
            news.NewsFeedUrl = newsFeedUrl;
          }
        }
      }
      catch (Exception ex) {
        Logger.Error(typeof(_default).Name + ".Page_Load", ex);
        Master.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    /// <summary>
    /// Handles the ItemDataBound event of the dgSearchTerms control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.Web.UI.WebControls.DataGridItemEventArgs"/> instance containing the event data.</param>
    void dgSearchTerms_ItemDataBound(object sender, DataGridItemEventArgs e) {
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
        e.Item.Attributes.Add("onmouseover", "this.className = \"over\";");
        e.Item.Attributes.Add("onmouseout", "this.className = this.className.replace(\"over\", \"\");");
      }
    }

    #endregion

    #region Methods

    #region Private

    /// <summary>
    /// Loads the search terms.
    /// </summary>
    private void LoadSearchTerms() {
      pnlSearchTerms.Visible = true;
      DataSet ds = new BrowsingLogController().FetchBrowsingLogSearchTerms();
      dgSearchTerms.DataSource = ds;
      dgSearchTerms.ItemDataBound += new DataGridItemEventHandler(dgSearchTerms_ItemDataBound);
      dgSearchTerms.Columns[0].HeaderText = LocalizationUtility.GetText("hdrSearchTerm");
      dgSearchTerms.Columns[1].HeaderText = LocalizationUtility.GetText("hdrCount");
      dgSearchTerms.DataBind();
    }

    /// <summary>
    /// Sets the defaul properties.
    /// </summary>
    private void SetDefaulProperties() {
      this.Title = LocalizationUtility.GetText("titleDashBoard");
      LocalizationUtility.AddPanelText(pnlNews);
      LocalizationUtility.AddPanelText(pnlQuickStats);
      LocalizationUtility.AddPanelText(pnlToDo);
      LocalizationUtility.AddLabelText(lblSearchTerms);
    }

    #endregion

    #endregion

  }
}
